﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.DesignPattern;
using Pixysoft.Framework.Logs.Controller;
using System.IO;
using Pixysoft.Framework.Logs.Core;
using Pixysoft.Framework.Configurations;

namespace Pixysoft.Framework.Logs
{
    public class Logger
    {
        private static volatile Logger instance;

        private static object syncRoot = new Object();

        protected static Logger Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new Logger();
                        }
                    }
                }
                return instance;
            }
        }

        protected Logger()
        {
            LogConfiguration config = Configuration.GetConfiguration<LogConfiguration>();

            if (config != null)
                Initializes(config);
        }

        public static bool Initialize()
        {
            LogConfiguration config = Configuration.GetConfiguration<LogConfiguration>();

            if (config != null)
                return Initialize(config);

            return false;
        }

        public static bool Initialize(LogConfiguration config)
        {
            return Instance.Initializes(config);
        }

        /// <summary>
        /// 返回默认的日志
        /// </summary>
        /// <returns></returns>
        public static ILogger GetLogger()
        {
            return GetLogger(StringHelper.default_log_prefix);
        }

        /// <summary>
        /// 返回制定文件名日志
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static ILogger GetLogger(string filename)
        {
            return GetLogger(filename, string.Empty);
        }

        /// <summary>
        /// 返回制定文件名和模块的日志
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="modulename"></param>
        /// <returns></returns>
        public static ILogger GetLogger(string filename, string modulename)
        {
            return Instance.GetLoggers(filename, modulename);
        }

        /// <summary>
        /// 返回制定文件名、模块的日志
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="module"></param>
        /// <returns></returns>
        public static ILogger GetLogger(string filename, Type module)
        {
            string modulename = module.Name;

            if (string.IsNullOrEmpty(modulename))
                modulename = module.FullName;

            return GetLogger(filename, modulename);
        }

        /// <summary>
        /// 解析制定文件为对象 传入文件全路径+文件名
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static ILogReport Parse(string fullfilename)
        {
            if (!File.Exists(fullfilename))
                return null;

            return LogReportController.Instance.ParseReport(fullfilename);
        }


        /// <summary>
        /// 加载配置文件
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        public bool Initializes(LogConfiguration config)
        {
            SettingController.Initialize(config);

            return true;
        }

        /// <summary>
        /// 返回制定文件名和模块的日志
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="modulename"></param>
        /// <returns></returns>
        public ILogger GetLoggers(string filename, string modulename)
        {
            return new CoreLogger(filename, modulename);
        }

    }
}
